﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Ajax.Web
{
    public partial class Product : Ajax.Tools.BasePage
    {
        public int pcount = 0; //总条数
        public int page; //当前页
        public readonly int pagesize = 20; //设置每页显示的大小

        public int classId;
        public string orderby = "Default_view";
        public string keywords = "";
        public string property = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["classId"] as string, out this.classId))
            {
                this.classId = 0;
            }
            if (!string.IsNullOrEmpty(Request.Params["orderby"]))
            {
                this.orderby = Request.Params["orderby"].Trim();
            }
            if (!string.IsNullOrEmpty(Request.Params["keywords"]))
            {
                this.keywords = Request.Params["keywords"].Trim();
            }
            if (!string.IsNullOrEmpty(Request.Params["property"]))
            {
                this.property = Request.Params["property"].Trim();
            }
            if (!Page.IsPostBack)
            {
                RptBind("IsLock=0 " + this.CombSqlTxt(this.classId, this.keywords, this.property), this.orderby);
            }
        }

        #region 列表绑定
        private void RptBind(string _where, string _orderby)
        {
            if (!int.TryParse(Request.Params["page"] as string, out this.page))
            {
                this.page = 0;
            }
            Ajax.BLL.Products bll = new Ajax.BLL.Products();
            //获得总条数
            this.pcount = bll.GetCount(_where);
            this.ddlOrderBy.SelectedValue = _orderby;
            string sortby = "AddTime desc";
            switch (_orderby)
            {
                case "Time_asc":
                    sortby = "AddTime asc";
                    break;
                case "Time_desc":
                    sortby = "AddTime desc";
                    break;
                case "Default_view":
                    sortby = "AddTime desc";
                    break;
            }
            //查询分页绑定数据
            this.rptList.DataSource = bll.GetPageList(this.pagesize, this.page, _where, sortby);
            this.rptList.DataBind();
        }
        #endregion

        #region 组合URL参数
        protected string CombKeywords(int _classId, string _orderby, string _keywords, string _property)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (_classId > 0)
            {
                strTemp.Append("classId=" + _classId + "&");
            }
            if (!string.IsNullOrEmpty(_orderby))
            {
                strTemp.Append("orderby=" +_orderby + "&");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("keywords=" + HttpUtility.UrlEncode(_keywords) + "&");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                strTemp.Append("property=" + _property + "&");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 组合查询语句
        protected string CombSqlTxt(int _classId, string _keywords, string _property)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (_classId > 0)
            {
                strTemp.Append(" and ClassId=" + _classId);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (Title like '%" + _keywords + "%' or Xinghao like '%" + _keywords + "%')");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                switch (_property)
                {
                    case "IsMsg":
                        strTemp.Append(" and IsMsg=1");
                        break;
                    case "IsTop":
                        strTemp.Append(" and IsTop=1");
                        break;
                    case "IsRed":
                        strTemp.Append(" and IsRed=1");
                        break;
                    case "IsHot":
                        strTemp.Append(" and IsHot=1");
                        break;
                    case "IsSlide":
                        strTemp.Append(" and IsSlide=1");
                        break;
                }
            }

            return strTemp.ToString();
        }
        #endregion

        protected void ddlOrderBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Product.html?" + this.CombKeywords(this.classId, this.ddlOrderBy.SelectedValue, this.keywords, this.property) + "page=0");
        }
    }
}
